1   /*
2    * Copyright (C) 2012 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import com.google.common.collect.Multisets.UnmodifiableMultiset;
21  
22  import java.util.Comparator;
23  import java.util.NavigableSet;
24  
25  /**
26   * Implementation of {@link Multisets#unmodifiableSortedMultiset(SortedMultiset)},
27   * split out into its own file so it can be GWT emulated (to deal with the differing
28   * elementSet() types in GWT and non-GWT).
29   * 
30   * @author Louis Wasserman
31   */
32  @GwtCompatible(emulated = true)
33  final class UnmodifiableSortedMultiset<E>
34      extends UnmodifiableMultiset<E> implements SortedMultiset<E> {
35    UnmodifiableSortedMultiset(SortedMultiset<E> delegate) {
36      super(delegate);
37    }
38  
39    @Override
40    protected SortedMultiset<E> delegate() {
41      return (SortedMultiset<E>) super.delegate();
42    }
43  
44    @Override
45    public Comparator<? super E> comparator() {
46      return delegate().comparator();
47    }
48  
49    @Override
50    NavigableSet<E> createElementSet() {
51      return Sets.unmodifiableNavigableSet(delegate().elementSet());
52    }
53  
54    @Override
55    public NavigableSet<E> elementSet() {
56      return (NavigableSet<E>) super.elementSet();
57    }
58  
59    private transient UnmodifiableSortedMultiset<E> descendingMultiset;
60  
61    @Override
62    public SortedMultiset<E> descendingMultiset() {
63      UnmodifiableSortedMultiset<E> result = descendingMultiset;
64      if (result == null) {
65        result = new UnmodifiableSortedMultiset<E>(
66            delegate().descendingMultiset());
67        result.descendingMultiset = this;
68        return descendingMultiset = result;
69      }
70      return result;
71    }
72  
73    @Override
74    public Entry<E> firstEntry() {
75      return delegate().firstEntry();
76    }
77  
78    @Override
79    public Entry<E> lastEntry() {
80      return delegate().lastEntry();
81    }
82  
83    @Override
84    public Entry<E> pollFirstEntry() {
85      throw new UnsupportedOperationException();
86    }
87  
88    @Override
89    public Entry<E> pollLastEntry() {
90      throw new UnsupportedOperationException();
91    }
92  
93    @Override
94    public SortedMultiset<E> headMultiset(E upperBound, BoundType boundType) {
95      return Multisets.unmodifiableSortedMultiset(
96          delegate().headMultiset(upperBound, boundType));
97    }
98  
99    @Override
100   public SortedMultiset<E> subMultiset(
101       E lowerBound, BoundType lowerBoundType,
102       E upperBound, BoundType upperBoundType) {
103     return Multisets.unmodifiableSortedMultiset(delegate().subMultiset(
104         lowerBound, lowerBoundType, upperBound, upperBoundType));
105   }
106 
107   @Override
108   public SortedMultiset<E> tailMultiset(E lowerBound, BoundType boundType) {
109     return Multisets.unmodifiableSortedMultiset(
110         delegate().tailMultiset(lowerBound, boundType));
111   }
112 
113   private static final long serialVersionUID = 0;
114 }